package academic_winter2014_inse6130_project;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;

import org.apache.commons.codec.binary.Base64;
 
public class CloudGPSsecurityEncryptor extends JFrame
{
	String encryption_streetno, encryption_streetname, encryption_city, encryption_province, encryption_postalcode;

	
	 String key1 = "PRATIK JOSHI    "; // 128 bit key
     String key2 = "inse6130_PRATIKj"; // This key would only be shared between authorized users.
	
	public static String encrypt(String key1, String key2, String txt) {
        try {
            IvParameterSpec iv = new IvParameterSpec(key2.getBytes("UTF-8"));

            SecretKeySpec skeySpec = new SecretKeySpec(key1.getBytes("UTF-8"), "AES");
           
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
           
            cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
           
            byte[] encrypted = cipher.doFinal(txt.getBytes());
     
            return Base64.encodeBase64String(encrypted);
       
        } catch (Exception ex) {
            ex.printStackTrace();
       
        }
       
        return null;
    }
	
	private static final long serialVersionUID = 1L;
	
    public void start()
    {
       
        final JTextField streetno=new JTextField(40);
        streetno.setText("Enter Destination Street/Building No here:");
        add(streetno);
        final JTextField streetname=new JTextField(40);
        streetname.setText("Enter Destination Street Name here:");
        add(streetname);
        final JTextField city=new JTextField(40);
        city.setText("Enter Destination City here:");
        add(city);
        final JTextField province=new JTextField(40);
        province.setText("Enter Destination Province here:");
        add(province);
        final JTextField postalcode=new JTextField(40);
        postalcode.setText("Enter Destination Postal Code here:");
        add(postalcode);

        final JTextField encrypted_streetno = new JTextField(40);
        add(encrypted_streetno);
        final JTextField encrypted_streetname = new JTextField(40);
        add(encrypted_streetname);
        final JTextField encrypted_city = new JTextField(40);
        add(encrypted_city);
        final JTextField encrypted_province = new JTextField(40);
        add(encrypted_province);
        final JTextField encrypted_postalcode = new JTextField(40);
        add(encrypted_postalcode);
      
        JButton ENCRYPT=new JButton();
        ENCRYPT.setText("Encrypt");
        add(ENCRYPT);
   
                ENCRYPT.addActionListener(
                new ActionListener(){
                    public void actionPerformed(
                            ActionEvent e){
                    						String str_streetno = streetno.getText();
                    						encryption_streetno = CloudGPSsecurityEncryptor.encrypt(key1,key2,str_streetno);
                    						encrypted_streetno.setText(encryption_streetno);
                    						
                    						String str_streetname = streetname.getText();
                    						encryption_streetname = CloudGPSsecurityEncryptor.encrypt(key1,key2,str_streetname);
                    						encrypted_streetname.setText(encryption_streetname);
                    						
                    						String str_city = city.getText();
                    						encryption_city = CloudGPSsecurityEncryptor.encrypt(key1,key2,str_city);
                    						encrypted_city.setText(encryption_city);
                    						
                    						String str_province = province.getText();
                       						encryption_province = CloudGPSsecurityEncryptor.encrypt(key1,key2,str_province);
                       						encrypted_province.setText(encryption_province);
             
                    						String str_postalcode = postalcode.getText();
                    						encryption_postalcode = CloudGPSsecurityEncryptor.encrypt(key1,key2,str_postalcode);
                    						encrypted_postalcode.setText(encryption_postalcode);
                                           
                    						CloudGPSsecurityEncryptor.logfilewriter("Encryption",encryption_streetno);
                    						CloudGPSsecurityEncryptor.logfilewriter("Encryption",encryption_streetname);
                    						CloudGPSsecurityEncryptor.logfilewriter("Encryption",encryption_city);
                    						CloudGPSsecurityEncryptor.logfilewriter("Encryption",encryption_province);
                    						CloudGPSsecurityEncryptor.logfilewriter("Encryption",encryption_postalcode);
                    }
                                    }
                            );
            
        setLayout(new FlowLayout());
        setSize(800,800);
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }
    
   
	public static void logfilewriter(String filename,String info){
		
		File file;
	    	BufferedWriter bufferedwritter = null;
	    	try{
	    		file = new File("C:\\Users\\Michele Toso\\Dropbox\\INSE6130_Project_Team\\Encryption.txt");
	    		if(!file.isFile()){
	    			file.createNewFile();
	    		}	
                FileWriter fileWriter = new FileWriter(file,true);
		        bufferedwritter = new BufferedWriter(fileWriter);
		        
		        String  message  = filename +" :"+  info;  
		        	
                        bufferedwritter.write(message);
                        bufferedwritter.newLine();
	    	}

	    	catch(IOException ioe){
	    		System.out.println("IO Exceptions " +ioe);
	    			    	}
			catch(Exception e){
				System.out.println(" Un Known Exceptions " +e);
				}
			finally{
				try {
					if(bufferedwritter!=null){
						bufferedwritter.close();
					}
				}
				catch (IOException e) {

					e.printStackTrace();
				}
			}	
	    }
    public static void main(String args[])
    {
        new CloudGPSsecurityEncryptor().start();
    }
}
